---
title: "Common Pitfalls & Debugging Guide"
description: "Learn common pitfalls in Rust development and how to use debugging tools and compiler hints to solve problems"
---

# Common Pitfalls & Debugging Guide

## 📖 Learning Objectives

This module will help you identify and avoid common pitfalls in Rust development, and master effective debugging techniques and tools to improve development efficiency.

---

## 🎯 Common Pitfalls

### 1. Ownership & Borrowing Pitfalls

- **Use After Move**: Attempting to use a variable after its ownership has been moved to another variable or function.
- **Mutable and Immutable Borrows**: Creating a mutable borrow while an immutable borrow is still active, or vice versa.
- **Dangling References**: Returning a reference to a variable that goes out of scope at the end of a function.

### 2. Lifetime Pitfalls

- **Lifetime Mismatch**: Passing references with different lifetimes to a function that expects them to have the same lifetime.
- **Returning References to Local Variables**: Attempting to return a reference to a variable created inside a function.

### 3. Asynchronous Programming Pitfalls

- **Blocking in Async Code**: Calling blocking functions within an async context, which can halt the entire executor.
- **Forgetting `.await`**: Forgetting to call `.await` on a future, which will prevent it from running.

### 4. Error Handling Pitfalls

- **Overusing `unwrap()` and `expect()`**: Using `unwrap()` or `expect()` in production code can lead to panics.
- **Ignoring `Result` and `Option`**: Not handling the `Err` or `None` variants of `Result` and `Option` types.

### 5. Performance Pitfalls

- **Unnecessary Cloning**: Cloning large objects instead of borrowing them.
- **Inefficient String Concatenation**: Using `+` to concatenate strings in a loop, which can be slow.

---

## 🛠️ Debugging Tools & Techniques

### 1. Compiler Hints

The Rust compiler provides detailed error messages and suggestions. Pay close attention to them.

### 2. `println!` Debugging

Use `println!` macros to print the values of variables at different points in your code.

### 3. Logging Libraries

Use logging libraries like `log` and `env_logger` for more structured and configurable logging.

### 4. Debuggers

Use debuggers like `gdb` or `lldb` with the `rust-gdb` or `rust-lldb` scripts for a better debugging experience.

### 5. Performance Profilers

Use tools like `perf` on Linux or Instruments on macOS to profile your code and identify performance bottlenecks.

---

## 📝 Summary

This module covered common pitfalls in Rust development and introduced various debugging tools and techniques. By understanding these pitfalls and using the right tools, you can write more robust and efficient Rust code.

---

**Continue Learning**: [Idiomatic Rust Style](./module-11-idiomatic-rust-style)
